﻿using AutoMapper;
using HamcoWcs.Contract;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.DeviceMsgQueues;
using HamcoWcs.Entitys.MesMsgQueues;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DeviceMsgQueues;
using HamcoWcs.Models.DeviceMsgs;
using HamcoWcs.Models.Devices;
using HamcoWcs.Service.Common;
using System;

namespace HamcoWcs.Service.Mes.Parsers
{
    [AutoInject(typeof(IMesMsgParser), InjectType.Scope)]
    public abstract class MesMsgParserBase : ScopeServiceBase, IMesMsgParser
    {
        protected readonly ILoggerService _logger;
        protected static string SHELF_POSITION_TYPE = "SHELF";
        protected static string PORT_POSITION_TYPE = "PORT";

        public abstract string EventName { get; }
        public abstract void HandleEvent(MesMsgQueue message);

        protected MesMsgParserBase(ILoggerService loggerService, WcsDbContext wcsDbContext, IMapper mapper) : base(wcsDbContext, mapper)
        {
            _logger = loggerService;
        }

        protected void AddSystemErrorLog(string xml, Exception ex)
        {
            _logger.AddSystemErrorLog($"{this.GetType().Name}:{ex.Message}\r\n{xml}");
        }

        /// <summary>
        /// 插入设备消息队列
        /// </summary>
        protected void InsertDeviceMsgInQueue(PconDto pcon, PconSendMsg pconSendMsg)
        {
            var deviceMsgQueueDto = new DeviceMsgQueueDto(pcon, pconSendMsg);
            var deviceMsgQueue = _mapper.Map<DeviceMsgQueue>(deviceMsgQueueDto);

            _wcsDbContext.DeviceMsgQueues.Add(deviceMsgQueue);
            _wcsDbContext.SaveChanges();
        }

    }
}
